<?php

class infoControlPage extends commonControlPage {
    public function managerModule() {
        $opt = $_REQUEST['opt'] ? $_REQUEST['opt'] : 'list';

        call_user_func(array($this,"{$opt}Action"));
    }

    public function listAction() {
        $total = $_ENV['db']->fetchCol("SELECT count(1) FROM info");

        $pager = new class_pager();
        $pager->setConfig((int)$_GET['page_no'],$total);
        $pager->setUrl("?app=info&mod=manager&page_no=tpt",'tpt');

        $lists = $_ENV['db']->fetchAll("SElECT * FROM info order by info_id desc limit ".$pager->getLimit()." offset ".$pager->getOffset());

        $this->view->lists = $lists;
        $this->view->pager = $pager->output();
        $this->view->headlink = '<a href="?app=info&mod=manager&opt=add">发布</a>';
        $this->showMainPage('admincp/info_list');
    }

    public function addAction() {
        if (POST && AJAX) {
            $data['title'] = preg_replace('/\s*/','',$_POST['title']);
            $data['content'] = htmlspecialchars(trim($_POST['content']));
            
            $length = mb_strlen($data['title']);
            if ($length==0 || $length > 200) {
                $this->outputJson(0,'标题应为1到20个字符');
            }

            if (!$data['content']) {
                $this->outputJson(0,'请填写内容');
            }

            $data['dateline'] = time();

            $setsql = $this->buildSql($data);

            $_ENV['db']->runSql("INSERT INTO info {$setsql}");
            $this->outputJson(1,'添加成功');
        }
        $this->showMainPage('admincp/info_modi');
    }

    public function modiAction() {

        $iid = (int)$_GET['iid'];

        $info = $_ENV['db']->fetch("SELECT * FROM info WHERE info_id = '{$iid}'");

        if (!$info) {
            $this->showMessage('请从列表中点击编辑','back');
        }

        if (POST && AJAX) {
            $data['title'] = preg_replace('/\s*/','',$_POST['title']);
            $data['content'] = htmlspecialchars(trim($_POST['content']));
            
            $length = mb_strlen($data['title']);
            if ($length==0 || $length > 200) {
                $this->outputJson(0,'标题应为1到20个字符');
            }

            if (!$data['content']) {
                $this->outputJson(0,'请填写内容');
            }

            $data['dateline'] = time();

            $setsql = $this->buildSql($data);

            $_ENV['db']->runSql("update info {$setsql} WHERE info_id = '{$iid}'");
            $this->outputJson(1,'修改成功');
        }

        $this->view->info = $info;
        $this->showMainPage('admincp/info_modi');
    }

    public function deleteAction() {
    }
}